<?php

namespace App\Http\Controllers;

use App\Http\Resources\InvitationCollection;
use App\Invitation;
use App\User;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
class ShareController extends Controller
{
    /**
     * @return array|string 微信分享所需配置文件
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxshare($code)
    {
        $app = Factory::officialAccount(config('wechat.official_account.default'));
        $app->jssdk->setUrl(env('APP_URL')."mobile?onlycode=".$code);
        return $app->jssdk->buildConfig(array('updateAppMessageShareData','updateTimelineShareData'), $debug = false, $beta = false, $json = false);
    }

    /**
     * @return \生成邀请码  我邀请的用户列表
     */
    public function onlycode()
    {
        $userinfo = request()->user();
        return new InvitationCollection($userinfo->invitations()->orderBy('created_at', 'desc')->paginate(6));
    }

    /**
     * @param $code 邀请码
     * @return mixed  用户昵称·头像
     */
   public function shareUser($code)
   {
       $userId = deInvitationCode($code);
       $userInfo = User::find($userId, ['name','avatar']);
       $userInfo['avatar'] = getUserAvatar($userInfo->avatar);
       return $userInfo;
   }
}
